package com.zoe.springcloud.service;/*
 *@date 2019/12/11 0011
 */

import com.alibaba.fastjson.JSONObject;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author xjr
 */
@Service(value = "ribbonService")
public class RibbonService {
    @Autowired
    private RestTemplate restTemplate;

    public final JSONObject jsonObject=new JSONObject();

    @HystrixCommand(fallbackMethod = "hystrixMethod")
    public String send(String message){
        return restTemplate.getForObject("http://provider/provide/hello?message="+message,String.class);
    }
//降级处理的方法必须要保证参数个数和类型和原方法一致，否则会出现com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: fallback method wasn't found
    public String hystrixMethod(String message){
        jsonObject.put("error","该服务当前无法访问");
        jsonObject.put("result",message);
        return jsonObject.toJSONString();
    }
}
